﻿using LaunchAllaround.Configuration;
using LaunchAllaround.DesignPattern.MVC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaunchAllaround.Pad
{
    /// <summary>
    /// Logique d'interaction pour PadMenu.xaml
    /// </summary>
    public partial class PadMenu : UserControl, View<PadModel>, Controller<PadModel>
    {

        private Model<PadModel> model;

        public PadMenu(PadModel model)
        {
            this.model = model;
            InitializeComponent();
        }
        public PadMenu()
        {
            this.model = new PadModel(this);
            InitializeComponent();
        }

        public void Refresh(RefreshEvent<PadModel> ev)
        {
            //listen to the model
        }

        public void SetModel(Model<PadModel> model)
        {
            this.model = model;
        }

        public Model<PadModel> GetModel()
        {
            return this.model;
        }

        private void BeginHighlight(object sender, MouseEventArgs e)
        {
            ((Image)sender).Opacity = 1;
        }

        private void StopHighlight(object sender, MouseEventArgs e)
        {
            ((Image)sender).Opacity = 0.5;
        }

        private void AddRowEvent(object sender, MouseButtonEventArgs e)
        {
            if (((PadModel)this.GetModel()).Rows < PadModel.MaxRows)
            {
                ((PadModel)this.GetModel()).Rows++;
            }
            else
            {
                Footer.myself.PrintError("You can't add a row, the maximal number has been reached");
            }
        }

        private void AddColumnEvent(object sender, MouseButtonEventArgs e)
        {
            if (((PadModel)this.GetModel()).Columns < PadModel.MaxColumns)
            {
                ((PadModel)this.GetModel()).Columns++;
            }
            else
            {
                Footer.myself.PrintError("You can't add a column, the maximal number has been reached");
            }
        }

        private void RemoveRowEvent(object sender, MouseButtonEventArgs e)
        {
            if (((PadModel)this.GetModel()).Rows > 1)
            {
                ((PadModel)this.GetModel()).Rows--;
            }
            else
            {
                Footer.myself.PrintError("You can't remove a row, the minimal number has been reached");
            }
        }

        private void RemoveColumnEvent(object sender, MouseButtonEventArgs e)
        {
            if (((PadModel)this.GetModel()).Columns > 1)
            {
                ((PadModel)this.GetModel()).Columns--;
            }
            else
            {
                Footer.myself.PrintError("You can't remove a column, the minimal number has been reached");
            }
        }

        private void ChangeColorEvent(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.ColorDialog colorDlg = new System.Windows.Forms.ColorDialog();
            colorDlg.AllowFullOpen = true;
            colorDlg.Color = System.Drawing.Color.FromArgb(((PadModel)this.GetModel()).Color.A, ((PadModel)this.GetModel()).Color.R, ((PadModel)this.GetModel()).Color.G, ((PadModel)this.GetModel()).Color.B);
            colorDlg.CustomColors = ConfigurationModel.myself.GetCustomColors();
            colorDlg.FullOpen = true;
            colorDlg.AnyColor = true;
            colorDlg.SolidColorOnly = false;
            
            if (colorDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Color clr2 = System.Windows.Media.Color.FromArgb(colorDlg.Color.A, colorDlg.Color.R, colorDlg.Color.G, colorDlg.Color.B);
                int[] customColors = colorDlg.CustomColors;
                ((PadModel)this.GetModel()).Color = clr2;
                ConfigurationModel.myself.SetCustomColors(colorDlg.CustomColors);
            }
        }
    }
}
